<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="店铺" prop="mtShopName">
        <el-select v-model="queryParams.mtShopld" size="small" placeholder="请选择店铺" clearable>
          <el-option v-for="item in mtShops" :label="item.name" :value="item.id" :key="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="入库单号" prop="itemsn">
        <el-input
          v-model="queryParams.itemsn"
          placeholder="请输入入库单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="ERP业务单号" prop="code" label-width="120">
        <el-input v-model="queryParams.code" placeholder="请输入ERP业务单号" clearable size="small"/>
      </el-form-item>
      <el-form-item label="入库类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择入库类型" clearable size="small">
          <el-option v-for="dict in whinTypeList" :label="dict.dictLabel" :value="dict.dictValue" :key="dict.dictValue"/>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="业务日期" prop="biztime">-->
<!--        <el-date-picker v-model="queryParams.biztime" type="date" value-format="yyyy-MM-dd" size="mini" placeholder="请选择业务日期" clearable style="width: 191px"/>-->
<!--      </el-form-item>-->
      <el-form-item label="入库时间" prop="mtCreatedtimeStart">
        <el-date-picker v-model="queryParams.mtCreatedtimeStart" size="mini" type="date" value-format="yyyy-MM-dd" clearable placeholder="开始时间"/> -
        <el-date-picker v-model="queryParams.mtCreatedtimeEnd" size="mini" type="date" value-format="yyyy-MM-dd" clearable placeholder="结束时间"/>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="whinList">
      <el-table-column label="店铺" align="center" prop="mtShopName" width="130"/>
<!--      <el-table-column label="所属组织" align="center" prop="orgName" width="135"/>-->
<!--      <el-table-column label="入库单号" align="center" prop="itemsn" width="125"/>-->
      <el-table-column label="入库类型" align="center" prop="type" width="90" :formatter="whinType"/>
      <el-table-column label="ERP类型" align="center" prop="erpType" width="90" :formatter="whinERPType"/>
      <el-table-column label="业务日期" align="center" prop="biztime" >
        <template slot-scope="scope">
<!--          <span>{{scope.row.biztime|formatDate}}</span>-->
          <span>{{parseTime(scope.row.biztime)}}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="库房编码" align="center" prop="mtWhCode" width="60"/>-->

<!--      <el-table-column label="金额" align="center" prop="money" width="80">-->
<!--        <template slot-scope="scope">-->
<!--          <span v-if="scope.row.money">{{scope.row.money}}元</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="税额" align="center" prop="tax" width="40"/>-->
<!--      <el-table-column label="不含税金额" align="center" prop="notaxmoney" width="75">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{scope.row.notaxmoney}}元</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="对方机构编码" align="center" prop="oppositeorgCode" width="90"/>
      <el-table-column label="对方机构" align="center" prop="oppositeorgName" width="110"/>
      <el-table-column label="状态" align="center" prop="status" width="70" :formatter="whinStatus"/>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="备注" align="center" prop="remark" width="180"/>
      <el-table-column label="操作" align="center" prop="" width="50">
        <template slot-scope="scope">
          <el-button icon="el-icon-info" type="text" size="mini" @click="getWhinInfo(scope.row.id)">
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情对话框 -->
    <el-dialog title="入库单详情" :visible.sync="open" width="95%" append-to-body>
      <el-form :model="queryParamsItem" ref="itemForm" label-width="80" :inline="true" class="modalform">
        <el-form-item label="入库单号:">
          <span>{{itemWhinCode}}</span>
        </el-form-item>
        <el-form-item label="所属组织:">
          <span>{{orgName}}</span>
        </el-form-item>
        <el-form-item label="供应商:">
          <span>{{suppliername}}</span>
        </el-form-item>
        <el-form-item label="总金额:">
          <span>{{money}}元</span>
        </el-form-item>
        <el-form-item label="税额:">
          <span>{{tax}}元</span>
        </el-form-item>
        <el-form-item label="不含税总金额:">
          <span>{{notaxmoney}}元</span>
        </el-form-item>
        <br>
        <el-form-item label="物品编码:" prop="goodsCode">
          <el-input v-model="queryParamsItem.goodsCode" size="small" placeholder="请输入物品编码" clearable/>
        </el-form-item>
        <el-form-item>
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="itemQuery">搜索</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="whinInfoList"  v-loading="loading1">
        <el-table-column label="美团仓库" align="center" prop="mtWhCode" width="65"/>
        <el-table-column label="物品编码" align="center" prop="goodsCode" width="80"/>
        <el-table-column label="物品名称" align="center" prop="goodsName"/>
        <el-table-column label="数量" align="center" prop="amount" width="70">
          <template slot-scope="scope">
            <span>{{scope.row.amount}}{{scope.row.baseunitName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="单价" align="center" prop="price" width="70">
          <template slot-scope="scope">
            <span>{{scope.row.price}}元</span>
          </template>
        </el-table-column>
        <el-table-column label="金额" align="center" prop="money" width="70">
          <template slot-scope="scope">
            <span>{{scope.row.money}}元</span>
          </template>
        </el-table-column>
        <el-table-column label="税率" align="center" prop="taxrate" width="40"/>
        <el-table-column label="ERP库房" align="center" prop="storeroomname" width="70"/>
        <el-table-column label="ERP库间" align="center" prop="storehousename" width="70"/>
        <el-table-column label="ERP产品" align="center" prop="productname" width="70"/>
        <el-table-column label="ERP物料" align="center" prop="spname" width="70"/>
        <el-table-column label="ERP数量" align="center" prop="erpWeight" width="70">
          <template slot-scope="scope">
            <span>{{scope.row.erpWeight}}{{scope.row.stockunitname}}</span>
          </template>
        </el-table-column>
        <el-table-column label="ERP单价" align="center" prop="erpPrice" width="60">
          <template slot-scope="scope">
            <span v-if="scope.row.erpPrice">{{scope.row.erpPrice}}元</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" width="170"/>
      </el-table>
      <pagination v-show="total1>0" :total="total1" :page.sync="queryParamsItem.pageNum" :limit.sync="queryParamsItem.pageSize" @pagination="getWhinViewInfo"/>
    </el-dialog>
  </div>
</template>

<script>
import { listWhin, getWhin, delWhin, addWhin, updateWhin, exportWhin } from "@/api/mt/whin";
import {listWhinItem} from '@/api/mt/whinItem'
import {listShop} from '@/api/mt/shop'

export default {
  name: "mtWhin",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      loading1:true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      total1:0,
      // 美团入库单主表格数据
      whinList: [],
      //详情数据
      whinInfoList:[],
      whinStatusList:[],
      whinTypeList:[],
      erpTypes:[],
      mtShops:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      currentId:null,
      orgName:null,
      itemWhinCode:null,
      money:null,
      tax:null,
      notaxmoney:null,
      suppliername:null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mtOrgid: null,
        mtBelongsorgid: null,
        mtRootorgid: null,
        orgId: null,
        orgName: null,
        mtWhCode: null,
        itemsn: null,
        oppositeorgType: null,
        oppositeorgCode: null,
        oppositeorgName: null,
        code: null,
        biztime: null,
        mtStatus: null,
        type: null,
        sourcesn: null,
        stockmoney: null,
        money: null,
        tax: null,
        notaxmoney: null,
        modifytime: null,
        version: null,
        thirdrelatedsn: null,
        strguid: null,
        departmentguid: null,
        supplierguid: null,
        suppliername: null,
        status: null,
        mtShopld:null,
        mtShopName:null,
      },
      queryParamsItem:{
        pageNum: 1,
        pageSize: 10,
        mtWhinid:null,
        goodsCode:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("mt_whin_type").then(response=>{
        this.whinTypeList=response.data;
    });
    this.getDicts("mt_whin_status").then(response=>{
        this.whinStatusList=response.data;
    });
    this.getDicts("in_form_type").then(response=>{
      this.erpTypes=response.data;
    });
    this.getMtShops();
  },
  filters: {
    //时间戳转换
    formatDate: function (value) {
      let date = new Date(value );
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      MM = MM < 10 ? ('0' + MM) : MM;
      let d = date.getDate();
      d = d < 10 ? ('0' + d) : d;
      let h = date.getHours();
      h = h < 10 ? ('0' + h) : h;
      let m = date.getMinutes();
      m = m < 10 ? ('0' + m) : m;
      let s = date.getSeconds();
      s = s < 10 ? ('0' + s) : s;
      return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
    }
  },
  methods: {
    getMtShops(){
      listShop().then(response=>{
        this.mtShops=response.rows;
      })
    },
    //入库单erp类型字典
    whinERPType(row,column){
      return this.selectDictLabel(this.erpTypes,row.erpType);
    },
    //入库单状态字典
    whinStatus(row,column){
      return this.selectDictLabel(this.whinStatusList,row.status);
    },
    //入库类型字典
    whinType(row,column){
      return this.selectDictLabel(this.whinTypeList,row.type)
    },
    getWhinInfo(id){
      this.queryParamsItem.pageNum=1;
      this.queryParamsItem.goodsCode=null;
      for (let i = 0; i < this.whinList.length; i++) {
        let item=this.whinList[i];
        if (item.id==id){
          this.orgName=item.orgName;
          this.itemWhinCode=item.itemsn;
          this.money=item.money;
          this.tax=item.tax;
          this.notaxmoney=item.notaxmoney;
          this.suppliername=item.suppliername;
        }
      }
      this.open=true;
      this.loading1=true;
      this.currentId=id;
      this.queryParamsItem.mtWhinid=id
      listWhinItem(this.queryParamsItem).then(response=>{
        if (response.code==200){
          this.whinInfoList=response.rows;
          this.total1=response.total;
          this.loading1=false;
        }
      })
    },
    getWhinViewInfo(){
      this.open=true;
      this.loading1=true;
      this.queryParamsItem.mtWhinid=this.currentId;
      listWhinItem(this.queryParamsItem).then(response=>{
        if (response.code==200){
          this.whinInfoList=response.rows;
          this.total1=response.total;
          this.loading1=false;
        }
      })
    },
    /** 查询美团入库单主列表 */
    getList() {
      this.loading = true;
      listWhin(this.queryParams).then(response => {
        this.whinList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        mtOrgid: null,
        mtBelongsorgid: null,
        mtRootorgid: null,
        orgId: null,
        orgName: null,
        mtWhCode: null,
        itemsn: null,
        oppositeorgType: null,
        oppositeorgCode: null,
        oppositeorgName: null,
        code: null,
        biztime: null,
        mtStatus: 0,
        type: null,
        sourcesn: null,
        mtCreatedtime: null,
        stockmoney: null,
        money: null,
        tax: null,
        notaxmoney: null,
        modifytime: null,
        version: null,
        thirdrelatedsn: null,
        strguid: null,
        departmentguid: null,
        supplierguid: null,
        suppliername: null,
        status: 0,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    //详情页搜索按钮操作
    itemQuery(){
      this.queryParamsItem.pageNum=1;
      this.getWhinViewInfo();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加美团入库单主";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWhin(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改美团入库单主";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWhin(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWhin(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delWhin(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有美团入库单主数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportWhin(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
